tunerfreemce = new ActiveXObject("TunerFreeMCE.TunerFreeControl");

var playState="Pause";
var fullScreenState="false";
var theTimeout;
var wakeInterval=tunerfreemce.wakeInterval();

function play(){
  if(playState!="Play"){


    if (fullScreenState=="true"){
      tunerfreemce.clickBBCPlayFS(screen.width,screen.height);
    } else {
      tunerfreemce.clickBBCPlayNonFS(screen.width,screen.height);
    }

    document.all.playButton.style.display="none";
    document.all.playButton.MCFocusable="false";
    document.all.pauseButton.style.display="inline";
    document.all.pauseButton.MCFocusable="true";
    setArray();
    startFocus();
    playState="Play";
    theTimeout=setTimeout("keepAwake()",wakeInterval);

    if (fullScreenState=="false"){
      setTimeout("fullScreen()",1000);
    }
  }
}

function pause(){

  if(playState!="Pause"){
  
    if (fullScreenState=="true"){
      tunerfreemce.clickBBCPlayFS(screen.width,screen.height);
    } else {
      tunerfreemce.clickBBCPlayNonFS(screen.width,screen.height);
    }
    
    document.all.playButton.style.display="inline";
    document.all.playButton.MCFocusable="true";
    document.all.pauseButton.style.display="none";
    document.all.pauseButton.MCFocusable="false";
    setArray();
    startFocus();
    playState="Pause";
    if (fullScreenState=="false"){
      setTimeout("focusHome(playButton)",1000);
    }
  }
}

function rev(){

    if (fullScreenState=="true"){
      tunerfreemce.clickBBCBackFS(screen.width,screen.height);
    } else {
      tunerfreemce.clickBBCBackNonFS(screen.width,screen.height);
    }
    
}

function stop(){
            focusHome(fsButton);
            fullScreenState="false";
            setTimeout("tunerfreemce.clickBBCPlayNonFS(screen.width,screen.height)",1000);
    playState="Stop";
    document.all.playButton.style.display="inline";
    document.all.playButton.MCFocusable="true";
    document.all.pauseButton.style.display="none";
    document.all.pauseButton.MCFocusable="false";
    setArray();
    startFocus();
      clearTimeout(theTimeout);
}

function fullScreen(){

  tunerfreemce.clickBBCFS(screen.width,screen.height);
  fullScreenState="true";
//setTimeout("focusHome(fsButton)",1000);
}

function switchResolution(){
   focusHome(fsButton);

  setTimeout("tunerfreemce.clickBBCSwitchFS(screen.width,screen.height)",1000);
  if(fullScreenState=="true"){
      setTimeout("fullScreen()",6000);
  }
}

function ageOK(){
   tunerfreemce.clickBBC16Check(screen.width,screen.height);

  setTimeout("tunerfreemce.clickBBC16OK(screen.width,screen.height)",1000);
  setTimeout("fullScreen()",5000);
}

function focusHome(oButton){
oButton.focus();
}

function keepAwake(){
  tunerfreemce.keepAwake();
  theTimeout=setTimeout("keepAwake()",wakeInterval);
}

/////////////////////////////////////////////////////////////////
// determine which remote control key the user selected 
// and take appropriate action
function onRemoteEvent(keyChar)
{
    /* Call optional "doOnFocus" function if needed; you can locate this function on the HTML page, and use it to tie some
   custom functionality to a remote-control keypress. If you want to stop onRemoteEvent from moving focus after that, 
   return true in the doOnFocus function to indicate that remote control keypress is already being handled in some other way */
    try
    {
        if (doOnFocus(keyChar) == true) 
        {
            return true;
        }
    }
    catch(e)
    {
        // if doOnFocus function is not present on page, ignore error
    }
    try
    {
    // this switch tests to see which button on the remote is pressed
        switch (keyChar)
        {
        case 179: // play/pause button
          if(playState=="Play"){
            pause();
          } else {
            play();
          }
        case 250: // play button
          play();
          break;
        case 19: // pause button
          pause();
          break;
        case 176:  // Skip button selected
            switchResolution();
            break;
        case 178:  // Stop button selected
            stop();
            break;
        case 177:  // Rev button selected
            rev();
            break;
        case 49:  // 1 button selected
            ageOK();
            break;
        case 38:  // Up button selected
            changeFocus("up");
            break;

        case 40:  // Down button selected
            changeFocus("down");
            break;

        case 37:  // Left button selected
            changeFocus("left");
            break;

        case 39:  // Right button selected
            changeFocus("right");
            break;

        case 13:    // Enter button selected, execute link to content/page
            doSelect();
            return true;
            break;

        case 8:  // Keyboard Backspace selected
            clearTimeout(theTimeout);
            if(playState!="Stop"){
              tunerfreemce.clickBBCPlayNonFS(screen.width,screen.height);
            }
            return false;
            break;

        case 166:             // Remote Control Back button selected; Media Center will already perform a Back
            clearTimeout(theTimeout);
            if(playState!="Stop"){
              tunerfreemce.clickBBCPlayNonFS(screen.width,screen.height);
            }
            return false;      // navigation when this is pressed, but this case can be used to add additional
            break;             //functionality to Back button

        case 33:    // Page up (plus) selected; page-up scrolling menu
            pageUpDown("up");
            return true;
            break;
            
        case 34:    // Page down (minus) selected; page-down scrolling menu
            pageUpDown("down");
            return true;
            break;

        default:
            return false;
            // ignore all other clicks
        }
    }
    catch(ex)
    {
        //ignore error
    }
    return true
}